package org.scisaga.tetra.core;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

public class GuardImpl {
	private ArrayList<EntiImpl> entityIn;
	private ArrayList<String> varList = new ArrayList<String>();
	private String selSql;
	private String delSql;
	protected Connection conn;
	private PreparedStatement stmt;
	private PreparedStatement stmt1;
	private ProcImpl proc;
	
	public static Messager mgr;
	String tempTableName;
	
	public GuardImpl(ProcImpl p){
		
		tempTableName = "T"+p.id.substring(0,6);
		
		proc = p;
		this.entityIn = proc.entityIn;
		
		//问题出在这里?
		conn = this.entityIn.get(0).conn;
		
		String field = "";
		String tableName = "";
		String ids = "";
		for(EntiImpl e:this.entityIn){
			tableName += e.name + ",";
			field += e.name + ".id AS "+e.name+"_id,";
			ids += e.name + ".id,";
			this.varList.addAll(e.varList.keySet());
		}
		for(String s:varList){
			field += s.substring(1, s.length()) + ",";
		}
		field = field.substring(0, field.length()-1);
		ids = ids.substring(0, ids.length()-1);
		tableName = tableName.substring(0, tableName.length()-1);
		
		selSql = "SELECT * FROM "+tempTableName;
		
		// 每次只选择一个的原因是关系代数中笛卡尔积的问题
		delSql = "CREATE TEMP TABLE "+tempTableName+" AS SELECT " + field + " FROM " + tableName + " WHERE " + proc.cond + " LIMIT 0,1;";
		for(EntiImpl e:this.entityIn){
			delSql += "DELETE FROM " + e.name + " WHERE " + e.name + ".id IN (SELECT " + e.name + "_id FROM "+tempTableName+");";
		}
		//delSql += "DROP TABLE "+this.proc.id+";";
		
	}
	
	public HashMap<String, String> getTokens(){
		// Prepare output token
		HashMap<String, String> token = new HashMap<String, String>();		
		ResultSet rs;
		
		try {
			//conn.setAutoCommit(false);
			
			
			stmt1 = conn.prepareStatement(delSql);
			stmt1.executeUpdate();
			
			stmt = conn.prepareStatement(selSql);
			rs = stmt.executeQuery();
			
			stmt1 = conn.prepareStatement("DROP TABLE "+tempTableName);
			stmt1.executeUpdate();
			
			if(rs.next()){
				// Get token
				for(String key : varList){
					token.put(key, rs.getString(key.substring(1, key.length())));
				}
				// TODO: 在记录不删除之前，会被其他的Process再取一次，有可能
				//for(EntiImpl e : this.entityIn){
				//	stmt1 = conn.prepareStatement("DELETE FROM " + e.name + " WHERE id = '" + rs.getString(e.name + ".id") + "'");
				//	//stmt1.executeUpdate("DELETE FROM " + e.name + " WHERE id = '" + rs.getString(e.name + ".id") + "'");
				//	stmt1.executeUpdate();
				//}
			} else {
				// No token got
			}
			//conn.commit();
			//conn.setAutoCommit(true);
			
		} catch (SQLException e) {
			e.printStackTrace();
			log("getToken() -> SQLException");
		}
		return token;
	}
	
	private static void log(String s){
		if(mgr != null){
			mgr.computeNode.log("Guard :: " + s);
		} else System.out.println("Guard :: " + s);
	}
}
